Maximum candies you can get from boxes¶
Time: O(N^2); Space: O(N); hard
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: * status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. * candies[i]: an integer representing the number of candies in box[i]. * keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. * containedBoxes[i]: an array contains the indices of the boxes found in box[i].
You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it.
Return the maximum number of candies you can get following the rules above.
Example 1:
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
Output: 16
Explanation:
You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don’t have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2.
In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed.
Total number of candies collected = 7 + 4 + 5 = 16 candy.
Example 2:
Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0]
Output: 6
Explanation:
You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6.
Example 3:
Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1]
Output: 1
Example 4:
Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = []
Output: 0
Example 5:
Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0]
Output: 7
Notes:
1 <= status.length <= 1000
status.length == candies.length == keys.length == containedBoxes.length == n
status[i] is 0 or 1.
1 <= candies[i] <= 1000
0 <= keys[i].length <= status.length
0 <= keys[i][j] < status.length
All values in keys[i] are unique.
0 <= containedBoxes[i].length <= status.length
0 <= containedBoxes[i][j] < status.length
All values in containedBoxes[i] are unique.
Each box is contained in one box at most.
0 <= initialBoxes.length <= status.length
0 <= initialBoxes[i] < status.length
Hint:
Use Breadth First Search (BFS) to traverse all possible boxes you can open. Only push to the queue the boxes the you have with their keys.
1. Breadth-first Search¶
Intuition From the description, we could visit a box if we have the box, and we have key to the box or the box is open. So I use has_key, can_visited and the given status to mark the status of a box. Once any status of a box is updated, we could check other status of the box to see if we could open it. If it is, we can add it to the queue.
This problem is interesting, because it is not actually a graph problem. But, we could use BFS to solve it.
[1]:
from collections import deque
class Solution1(object):
def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes):
"""
:type status: List[int]
:type candies: List[int]
:type keys: List[List[int]]
:type containedBoxes: List[List[int]]
:type initialBoxes: List[int]
:rtype: int
"""
n = len(status)
visited = [False] * n
has_key = [False] * n
can_visited = [False] * n
status = [bool(i) for i in status]
queue = deque(initialBoxes)
for box in initialBoxes:
visited[box] = True
while queue:
current = queue.popleft()
for child in keys[current]:
has_key[child] = True
for child in containedBoxes[current]:
can_visited[child] = True
for child in keys[current] + containedBoxes[current]:
if visited[child]:
continue
if (has_key[child] or status[child]) and can_visited[child]:
visited[child] = True
queue.append(child)
total_candies = 0
for i in range(n):
if visited[i]:
total_candies += candies[i]
return total_candies
[2]:
s = Solution1()
status = [1,0,1,0]
candies = [7,5,4,100]
keys = [[],[],[1],[]]
containedBoxes = [[1,2],[3],[],[]]
initialBoxes = [0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 16
status = [1,0,0,0,0,0]
candies = [1,1,1,1,1,1]
keys = [[1,2,3,4,5],[],[],[],[],[]]
containedBoxes = [[1,2,3,4,5],[],[],[],[],[]]
initialBoxes = [0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 6
status = [1,1,1]
candies = [100,1,100]
keys = [[],[0,2],[]]
containedBoxes = [[],[],[]]
initialBoxes = [1]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 1
status = [1]
candies = [100]
keys = [[]]
containedBoxes = [[]]
initialBoxes = []
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 0
status = [1,1,1]
candies = [2,3,2]
keys = [[],[],[]]
containedBoxes = [[],[],[]]
initialBoxes = [2,1,0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 7
[5]:
import collections
class Solution2(object):
"""
Time: O(n^2)
Space: O(n)
"""
def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes):
"""
:type status: List[int]
:type candies: List[int]
:type keys: List[List[int]]
:type containedBoxes: List[List[int]]
:type initialBoxes: List[int]
:rtype: int
"""
result = 0
q = collections.deque(initialBoxes)
while q:
changed = False
for _ in range(len(q)):
box = q.popleft()
if not status[box]:
q.append(box)
continue
changed = True
result += candies[box]
for contained_key in keys[box]:
status[contained_key] = 1
for contained_box in containedBoxes[box]:
q.append(contained_box)
if not changed:
break
return result
[6]:
s = Solution2()
status = [1,0,1,0]
candies = [7,5,4,100]
keys = [[],[],[1],[]]
containedBoxes = [[1,2],[3],[],[]]
initialBoxes = [0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 16
status = [1,0,0,0,0,0]
candies = [1,1,1,1,1,1]
keys = [[1,2,3,4,5],[],[],[],[],[]]
containedBoxes = [[1,2,3,4,5],[],[],[],[],[]]
initialBoxes = [0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 6
status = [1,1,1]
candies = [100,1,100]
keys = [[],[0,2],[]]
containedBoxes = [[],[],[]]
initialBoxes = [1]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 1
status = [1]
candies = [100]
keys = [[]]
containedBoxes = [[]]
initialBoxes = []
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 0
status = [1,1,1]
candies = [2,3,2]
keys = [[],[],[]]
containedBoxes = [[],[],[]]
initialBoxes = [2,1,0]
assert s.maxCandies(status, candies, keys, containedBoxes, initialBoxes) == 7